<?php
/**
 * @version 	$Id: bootstrap.php 114 2010-05-22 04:03:54Z mylifeisskidrow $
 * @author 		$Author: mylifeisskidrow@gmail.com $
 * @copyright 	Copyright (c) 2010 Luu Trong Hieu. All right reserved.
 * @license 	GNU/GPL see license.php
 */

defined('IN_FLYWHEEL') or die('Restrictedd Access!');

/**
 * bootstap
 * 	bootstrap application
 */

//Define
define('_ROOT_PATH_', dirname(__FILE__)._DS_);
define('_PROJECT_CONFIGURATION_PATH_', _ROOT_PATH_.'config'._DS_);
define('_CACHE_PATH_', _ROOT_PATH_.'cache'._DS_);
define('_LOG_PATH_', _ROOT_PATH_.'logs'._DS_);
define('_SYSTEMS_PATH_', _ROOT_PATH_.'systems'._DS_);
define('_APPS_PATH_', _ROOT_PATH_.'apps'._DS_);
define('_LANGUAGES_PATH_', _ROOT_PATH_.'languages'._DS_);
define('_VENDORS_PATH_', _ROOT_PATH_.'vendors'._DS_);
define('_MODELS_PATH_', _ROOT_PATH_.'models'._DS_);
define('_WEBSITE_NAME_', 'FLYWHEEL CMS');
define('_WEBSITE_LINK_', 'http://localhost/flywheel/');

class fBoostraper {
	public static function loadDoctrine() {
		require_once _VENDORS_PATH_.'doctrine'._DS_.'Doctrine.php';
//		require_once _VENDORS_PATH_.'doctrine'._DS_.'Doctrine.compiled.php';		
		spl_autoload_register(array('Doctrine', 'autoload'));
		spl_autoload_register(array('Doctrine', 'modelsAutoload'));		
		$manager = Doctrine_Manager::getInstance();
		$manager->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_ALL);
	    $manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_NONE);
	    $manager->setAttribute(Doctrine::ATTR_RECURSIVE_MERGE_FIXTURES, true);
	    $manager->setAttribute(Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
	    $manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, true);
	    $manager->setAttribute(Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_CONSERVATIVE);	
	    // apply default attributes
	    $manager->setDefaultAttributes();
	    Doctrine::loadModels(_MODELS_PATH_);
	    fDatabase::getConnection();
//	    Doctrine_Core::generateModelsFromDb(_MODELS_PATH_);
//		Doctrine_Core::generateYamlFromDb(_ROOT_PATH_.'database'._DS_.'schema.yml');
//		Doctrine_Core::generateYamlFromModels(_ROOT_PATH_.'database', _MODELS_PATH_);	    
	}
}

require_once _SYSTEMS_PATH_.'autoload'._DS_.'fSystemAutoload.class.php';
fSystemAutoload::register();

/**
 * Load System
 *
 */
function bootstrapping($name) {	
//	fLoader::import('files_system.fFolder', 'systems', 'helper.php');
	$enviroment = fConfig::get('enviroment');		
			
	if ($enviroment == fApplication::ENV_PROD || $enviroment == fApplication::ENV_TEST) {
		fFolderHelper::create($path = _CACHE_PATH_.$name._DS_.'core');			
		$compileFile = $path._DS_.'core_compile.php';
		if (!file_exists($compileFile)) {
			$str = file_get_contents(_SYSTEMS_PATH_.'application'._DS_.'fRouter.class.php');
			$str = file_get_contents(_SYSTEMS_PATH_.'databases'._DS_.'fDB.class.php');			
			$str .= file_get_contents(_SYSTEMS_PATH_.'controller'._DS_.'fController.class.php');
			$str .= file_get_contents(_SYSTEMS_PATH_.'environment'._DS_.'fRequest.class.php');
			$str .= file_get_contents(_SYSTEMS_PATH_.'environment'._DS_.'fResponse.class.php');
			$str .= file_get_contents(_SYSTEMS_PATH_.'environment'._DS_.'fUri.class.php');
			$str .= file_get_contents(_SYSTEMS_PATH_.'user'._DS_.'fUser.class.php');
			$str .= file_get_contents(_SYSTEMS_PATH_.'session'._DS_.'fSession.class.php');
			$str .= file_get_contents(_SYSTEMS_PATH_.'filter'._DS_.'fFilter.class.php');
			$str .= file_get_contents(_SYSTEMS_PATH_.'i18n'._DS_.'fI18N.class.php');
			$str .= file_get_contents(_SYSTEMS_PATH_.'exception'._DS_.'fException.class.php');
			$str .= file_get_contents(_SYSTEMS_PATH_.'exception'._DS_.'fDatabaseException.class.php');
			$str .= file_get_contents(_SYSTEMS_PATH_.'exception'._DS_.'fConfigException.class.php');
			$str .= file_get_contents(_SYSTEMS_PATH_.'exception'._DS_.'fControllerException.class.php');
			$str .= file_get_contents(_SYSTEMS_PATH_.'exception'._DS_.'fError404Exception.class.php');
			$str .= file_get_contents(_SYSTEMS_PATH_.'exception'._DS_.'fConfigException.class.php');
			$str .= file_get_contents(_SYSTEMS_PATH_.'document'._DS_.'fHtmlDocument.class.php');
			$str .= file_get_contents(_SYSTEMS_PATH_.'form'._DS_.'fForm.class.php');
			$str = str_replace("<?php", '', $str);				
			$str = ereg_replace("/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/", '', $str);				
			$str = ereg_replace("#[\x20-\x7E]*\n", '', $str);
			$str = ereg_replace("\t|\n", '', $str);
						
			$str = '<?php ' .$str;
			file_put_contents($compileFile, $str);				
		}			
		require_once $compileFile;	
	}
	else {
		require_once _SYSTEMS_PATH_.'config'._DS_.'fApplicationConfiguration.class.php';							
//		fLoader::import('application.fRouter');
//		fLoader::import('databases.fDB');		
//		fLoader::import('controller.fController');
//		fLoader::import('controller.fModule');
//		fLoader::import('environment.fRequest');
//		fLoader::import('environment.fResponse');
//		fLoader::import('environment.fUri');
//		fLoader::import('user.fUser');
//		fLoader::import('session.fSession');
//		fLoader::import('filter.fFilter');
//		fLoader::import('i18n.fI18N');
//		fLoader::import('exception.fException');
//		fLoader::import('exception.fDatabaseException');
//		fLoader::import('exception.fConfigException');
//		fLoader::import('exception.fControllerException');
//		fLoader::import('exception.fError404Exception');			
//		fLoader::import('document.fHtmlDocument');			
//		fLoader::import('form.fForm');				
	}
	
	if (fConfig::get('debug')) {		
		fProfiler::getInstance()->mark('Bootstrap application');
	}
}